from flask import Flask, render_template, request, jsonify
import sqlite3
import os

app = Flask(__name__)

# 初始化数据库
DATABASE = 'data.db'



@app.route('/')
def index():
    return render_template('index.html')

@app.route('/search')
def search():
    query = request.args.get('q', '')
    print(query)
    conn = sqlite3.connect(DATABASE)
    c = conn.cursor()
    c.execute("UPDATE codes SET search_count = search_count + 1 WHERE chinese_name LIKE ?", ('%'+query+'%',))
    conn.commit()
    c.execute("SELECT chinese_name, game_code FROM codes WHERE chinese_name LIKE ? ORDER BY search_count DESC", ('%'+query+'%',))
    results = [{'name': row[0], 'code': row[1]} for row in c.fetchall()]
    print(results)
    conn.close()
    return jsonify(results)

@app.route('/map')
def show_map():
    return render_template('map.html')

if __name__ == '__main__':
    app.run(debug=True)